// src/api/auth.js
import axios from 'axios'

const service = axios.create({
    baseURL: "http://localhost:80/api",
    timeout: 5000
})

// 请求拦截器
service.interceptors.request.use(
    config => config,
    error => Promise.reject(error)
)

// 响应拦截器
service.interceptors.response.use(
    response => response.data,
    error => {
        if (error.response) {
            const status = error.response.status
            const message = error.response.data?.message || '请求错误'
            return Promise.reject(new Error(`[${status}] ${message}`))
        }
        return Promise.reject(new Error(error.message || '网络错误'))
    }
)




// 登录方法
export const login = (data) => {
    return service({
        url: '/auth/login',
        method: 'post',
        data
    })
}


//Token验证方法
export const validateToken = (accessToken) => {
    return service({
        url: '/auth/validate',
        method: 'post',
        headers:{
            'Authorization': `Bearer ${accessToken}`
        }
    })
}

//获取文章
export const getArticle = (accessToken) => {
    return service({
        url:'/article/get',
        method:'get',
        headers:{
            'Authorization': `Bearer ${accessToken}`
        }
    })
}

export const pushArticle = (accessToken , data) =>{
    return service({
        url:'/article/add',
        method:'post',
        headers:{
            'Authorization': `Bearer ${accessToken}`
        },
        data
    })
}


